﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;

namespace XiaofangExamFramework.ServiceApi
{
    public class KaoshiApi
    {
        public static TData<List<ShitiEntity>> Search(string keyword)
        {
            //string url = "http://localhost:5001/kaoshi/search?keyword=" + keyword;
            string url = "http://api.kaoshi.sdsnwl.com/kaoshi/search?keyword=" + keyword;

            Debug.WriteLine(url);
            string response = Get(url);
            if (!string.IsNullOrEmpty(response))
            {
                return JsonConvert.DeserializeObject<TData<List<ShitiEntity>>>(response);
            }
            return null;
        }
        public static TData<List<ShitiEntity>> Search(string keyword,string name)
        {
            //string url = "http://localhost:5001/kaoshi/search?keyword=" + keyword;
            string url = $"http://api.kaoshi.sdsnwl.com/kaoshi/search?keyword={keyword}&userkey={name}";

            string response = Get(url);
            if (!string.IsNullOrEmpty(response))
            {
                return JsonConvert.DeserializeObject<TData<List<ShitiEntity>>>(response);
            }
            return null;
        }
        /// <summary>
        /// 处理http GET请求，返回数据
        /// </summary>
        /// <param name="url">请求的url地址</param>
        /// <returns>http GET成功后返回的数据，失败抛WebException异常</returns>
        private static string Get(string url)
        {
            System.GC.Collect();
            string result = "";

            HttpWebRequest request = null;
            HttpWebResponse response = null;

            //请求url以获取数据
            try
            {
                //设置最大连接数
                ServicePointManager.DefaultConnectionLimit = 200;

                /***************************************************************
                * 下面设置HttpWebRequest的相关属性
                * ************************************************************/
                request = (HttpWebRequest)WebRequest.Create(url);

                request.Method = "GET";

                ////设置代理
                //WebProxy proxy = new WebProxy();
                //proxy.Address = new Uri(WxPayConfig.PROXY_URL);
                //request.Proxy = proxy;

                //获取服务器返回
                response = (HttpWebResponse)request.GetResponse();

                //获取HTTP返回数据
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd().Trim();
                sr.Close();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                System.Threading.Thread.ResetAbort();
            }
            catch (Exception e)
            {
                //throw new Exception(e.Message.ToString());
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return result;
        }
    }
}
